﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sicurezza.Client.Manager;
using Sicurezza.Entita;

namespace Sicurezza.Client.Controlli
{
    public partial class OpenForm : Form
    {
        public enum Mode { Request, Certificate, Renew, Recipient };

        public string SelectedId { get; set; }
        public string SelectedDN { get; set; }
        public Pair SelectedPair { get; set; }

        public object DataSource {
            get {
                return listBox1.DataSource;
            }
            set {
                listBox1.DataSource = value;
                listBox1.DisplayMember = "value";
                listBox1.ValueMember = "key";
            } }

        public bool Re { get; set; }

        public OpenForm(Mode om)
        {
            InitializeComponent();
            switch (om)
            {
                case Mode.Request:
                    listBox1.DataSource = RequestManager.GetAll();
                    Text = "Open Request";
                    break;
                case Mode.Certificate:
                    listBox1.DataSource = X509Manager.GetAll();
                    Text = "Open Certificate";
                    break;
                case Mode.Renew:
                    listBox1.DataSource = RenewManager.GetAll();
                    Text = "Open Renew";
                    break;
                case Mode.Recipient:
                    Text = "Add Recipient";
                    break;
                default:
                    break;
            }
            listBox1.DisplayMember = "value";
            listBox1.ValueMember = "key";

        }

        private void openButton_Click(object sender, EventArgs e)
        {
            Bind();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            Bind();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void Bind()
        {
            SelectedId = Convert.ToString(listBox1.SelectedValue);
            SelectedDN = Convert.ToString(listBox1.Text);
            SelectedPair = listBox1.SelectedItem as Pair;
        }
    }
}
